package com.chromatic.competition.view.profile;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.chromatic.competition.BuildConfig;
import com.chromatic.competition.R;
import com.chromatic.competition.service.MusicService;

public class SettingsActivity extends AppCompatActivity {
    private Button playButton;
    private boolean isPlaying = false;
    private Intent serviceIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        playButton = findViewById(R.id.play_button);
        serviceIntent = new Intent(this, MusicService.class);

        playButton.setOnClickListener(v -> {
            if (isPlaying) {
                stopService(serviceIntent); // 停止背景音乐服务
                isPlaying = false;
            } else {
                startService(serviceIntent); // 启动背景音乐服务
                isPlaying = true;
            }
            updatePlayButton();
        });

        ImageView backButton = findViewById(R.id.backButton);
        backButton.setOnClickListener(v -> {
            onBackPressed(); // 调用系统返回操作
        });

        TextView versionTextView = findViewById(R.id.version_textview);
        String versionName = BuildConfig.VERSION_NAME;
        String releaseDate = BuildConfig.APP_RELEASE_DATE;
        String formattedVersion = getString(R.string.app_version, versionName, releaseDate);
        versionTextView.setText(formattedVersion);

    }
    private void updatePlayButton() {
        if (isPlaying) {
            playButton.setText("暂停播放背景音乐");
            playButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_music_off_24dp, 0, 0, 0);
        } else {
            playButton.setText("播放背景音乐");
            playButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_music_note_24dp, 0, 0, 0);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 检查服务是否在运行，更新播放按钮的状态
        isPlaying = isServiceRunning();
        updatePlayButton();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 根据播放状态启动或停止服务
        if (isPlaying) {
            startService(serviceIntent);
        } else {
            stopService(serviceIntent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG","---On Destroy---:SettingsActivity摧毁");
    }

    // 检查背景音乐服务是否在运行
    private boolean isServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (MusicService.class.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}